package com.example.smartcity1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.NewsBean;

import java.util.List;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class NewsTopicAdapter extends ArrayAdapter<NewsBean.RowsDTO> {
    public NewsTopicAdapter(@NonNull Context context, int resource, @NonNull List<NewsBean.RowsDTO> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_news_topic,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.item_topic_img);
            viewHolder.textView = convertView.findViewById(R.id.item_topic_txt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        NewsBean.RowsDTO rowsDTO = getItem(position);
        viewHolder.textView.setText(rowsDTO.getTitle());
        Glide.with(getContext()).load(IP + rowsDTO.getImgUrl())
                .placeholder(R.drawable.ic_baseline_cloud_download_24)
                .centerCrop()
                .into(viewHolder.imageView);
        return convertView;
    }
    private class ViewHolder {
        ImageView imageView;
        TextView textView;
    }
}








